//
//  ZPMineConfirmRefundVC.m
//  ZPIOSEducation
//
//  Created by mini1 on 2017/1/16.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "ZPMineConfirmRefundVC.h"
#import "ZPMineClassViewController.h"
#import "InputButton.h"
#import "ZPMineClassChangeRecordVC.h"
@interface ZPMineConfirmRefundVC ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *className;
@property (strong,nonatomic) NSArray *reasonArray;
@property (weak, nonatomic) IBOutlet UILabel *quitNum;
@property (weak, nonatomic) IBOutlet UILabel *quitCost;
@property (weak, nonatomic) IBOutlet UIView *reasonView;
@property (strong, nonatomic) IBOutlet UIPickerView *reasonPicker;
@property (strong, nonatomic) IBOutlet UIToolbar *reasonToolbar;
@property (weak, nonatomic) IBOutlet InputButton *reasonBtn;
@property (weak, nonatomic) IBOutlet UITextView *reasonTextView;
@property (strong,nonatomic) NSDictionary *selectedReason;
@property (weak, nonatomic) IBOutlet UILabel *miscCost;
@end

@implementation ZPMineConfirmRefundVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    hud = [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:hud];
    _reasonBtn.inputView = _reasonPicker;
    _reasonBtn.inputAccessoryView = _reasonToolbar;
    _reasonView.hidden=YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChange:) name:UITextViewTextDidChangeNotification object:nil];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
}

- (void)textViewDidChange:(NSNotification *)sender
{
    NSInteger number = [((UITextView *)sender.object).text length];
    if (number > 50) {
        ((UITextView *)sender.object).text = [((UITextView *)sender.object).text substringToIndex:50];
        number = 50;
        [ZPPromptView showMessage:@"其他原因限50个字"];
    }
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.title = @"确认退班";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zhenpin_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClick)];
    _reasonArray = [_result objectForKey:@"reasonSelect"];
    _quitCost.text = [ NSString stringWithFormat:@"¥%@",[_result[@"qcVo"] objectForKey:@"quitCost"]];
    _quitNum.text = [NSString stringWithFormat:@"剩余课节数：%@", [_result  objectForKey:@"quitNum"]];
    if ([toStr([_result[@"qcVo"] objectForKey:@"miscCost"]) isEqualToString:@"0"]) {
        _miscCost.hidden=YES;
    }else{
        _miscCost.text =[NSString stringWithFormat:@"已扣除学杂费¥%@",toStr([_result[@"qcVo"] objectForKey:@"miscCost"])];
    }
    _className.text = [NSString stringWithFormat:@"%@", [_result[@"qcVo"] objectForKey:@"className"]];
}
- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)reasonBtnA:(InputButton *)sender {
    [sender becomeFirstResponder];
    if (_selectedReason==nil) {
        [self pickerView:_reasonPicker didSelectRow:0 inComponent:0];
    }

}
- (IBAction)cancelItem:(UIBarButtonItem *)sender {
    [_reasonBtn resignFirstResponder];

}
- (IBAction)confirmItem:(UIBarButtonItem *)sender {
    [_reasonBtn resignFirstResponder];
}


- (IBAction)confirmRefundClick:(id)sender
{
    // 试图pop一个已经不存在的视图控制器 解决办法:
    for (UIViewController * vc in self.navigationController.viewControllers) {
        if ([vc isKindOfClass:[ZPMineClassViewController class]]) {
            ZPMineClassViewController * viewController = (ZPMineClassViewController *)vc;
            [self.navigationController popToViewController:viewController animated:YES];
        }
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return _reasonArray.count;
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return toStr([_reasonArray[row] objectForKey:@"name"]);
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    _selectedReason = [_reasonArray objectAtIndex:row];
    [_reasonBtn setTitle:[_selectedReason objectForKey:@"name"] forState:UIControlStateNormal];
    _reasonView.hidden = ![[_selectedReason objectForKey:@"flagInfo"] boolValue];
}
- (IBAction)doneBtn:(UIButton *)sender {
    if (_selectedReason==nil) {
        [ZPPromptView showMessage:@"请输入详细退班原因"];
    } else if ([[_selectedReason objectForKey:@"flagInfo"] boolValue]&&(_reasonTextView.text.length==0)){
        [ZPPromptView showMessage:@"其他原因限50个字"];
    }else{
        [self createqc];
    }
}

-(void)createqc{
    [hud showAnimated:YES];
    [[ZPNetworking sharedZPNetworking] GET:APIUC@"quitclass/createqc.json" parameters:@{@"stuId":[[[ZPAuthStore sharedZPAuthStore] userToDic] objectForKey:@"stuId"], @"classId":_classId,@"miscCost":[[_result objectForKey:@"qcVo"] objectForKey:@"miscCost"],@"signCost":[[_result objectForKey:@"qcVo"] objectForKey:@"signCost"],@"quitCost":[[_result objectForKey:@"qcVo"] objectForKey:@"quitCost"],@"flagRate":[_selectedReason objectForKey:@"flagRate"],@"reasonId":[_selectedReason objectForKey:@"id"], @"reasons":toStr((_reasonTextView.text))} progress:nil
                                   success:^(id  _Nonnull responseObject) {
        [hud hideAnimated:YES];
        if (CodeIs200(responseObject)) {
            NSDictionary *result = [responseObject objectForKey:@"result"];
            if ([[result objectForKey:@"isOk"] boolValue]) {
                [ZPPromptView showMessage:toStr([result objectForKey:@"msg"])];
                ZPMineClassChangeRecordVC *vc=[[ZPMineClassChangeRecordVC alloc]init];
                vc.needPopToRoot=YES;
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                [ZPPromptView showMessage:toStr([result objectForKey:@"msg"])];
            }
        }else{
            [ZPPromptView showMessage:toStr([responseObject objectForKey:@"codeMsg"])];
            
        }
    } failure:^(NSError * _Nonnull error) {
        [ZPPromptView showMessage:error.localizedDescription];
        [hud hideAnimated:YES];
    }];
}


@end
